﻿using ASmile.Com;
using ASmile.Com.RoleUser;
using ASmile.DBModel;
using ASmile.Extend;
using System;
using System.Windows.Forms;

namespace ASmile.Win.Forms
{
    public partial class FrmRoleUser : FrmBase
    {
        public FrmRoleUser()
        {
            InitializeComponent();
        }

        IRoleUserBLL BLLObj { get; } = ObjectFactory.GetObject<IRoleUserBLL>("RoleUserBLL");

        ArgPageData SearchArg { get; } = new ArgPageData();

        ArgRoleUserSearch SearchDetailArg { get; } = new ArgRoleUserSearch();

        string _CurrRoleId;

        void RefreshRoleData()
        {
            _CurrRoleId = null;
            var ret = BLLObj.GetRoleData(tsTxtRoleSearch.Text);
            dgvRole.DataSource = ret.data;
        }

        void RefreshUserData()
        {
            SearchDetailArg.RoleId = _CurrRoleId;
            SearchDetailArg.SearchVal = tsTxtSearch.Text;

            if (_CurrRoleId != null)
            {
                var ret = BLLObj.GetRoleUsers(SearchDetailArg);
                //_CurrRoleId, tsTxtSearch.Text);
                dgvEmp.DataSource = ret.data;
            }
        }

        private void FrmUserRole_Load(object sender, EventArgs e)
        {
            RefreshRoleData();
        }

        private void dgvRole_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            var role = dgvRole.GetSelectedClassData<Sys_Role>();
            _CurrRoleId = role?.Id;
            RefreshUserData();
        }

        private void tsBtnSearch_Click(object sender, EventArgs e)
        {
            SearchArg.ClearADList();
            RefreshUserData();
        }

        private void tsTxtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                tsBtnSearch_Click(sender, e);
            }
        }

        private void tsBtnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tsBtnAddEmp_Click(object sender, EventArgs e)
        {
            if (_CurrRoleId.IsEmpty())
            {
                ShowMessage("没有选中角色");
                return;
            }
            FrmSelectUser frmSelect = null;
            frmSelect = FrmSelectUser.Create(AddUser);
            frmSelect.TopLevel = true;
            frmSelect.Show();

            void AddUser(Sys_User user)
            {
                var ret = BLLObj.AddUsers(new ArgRoleUserAdd
                {
                    RoleId = _CurrRoleId,
                    UserIds = new[] { user.Id }
                });//_CurrRoleId, user.Id);               
                if (ret)
                {
                    RefreshUserData();
                }
                ShowMessage(ret.msg);
                //if (frmSelect != null && !frmSelect.IsDisposed)
                //{
                //    frmSelect.ShowMessage(ret.msg);
                //}
            }

        }

        private void tsBtnRemoveEmp_Click(object sender, EventArgs e)
        {
            var sel = dgvEmp.GetSelectedClassData<Sys_RoleUser>();
            if (sel == null)
            {
                ShowMessage("没有选中任何记录");
                return;
            }
            if (!ShowQuestion("确实要移除人员吗?", "移除确认"))
            {
                return;
            }
            var ret = BLLObj.Remove(new ArgId(sel.Id));
            if (ret)
            {
                RefreshUserData();
            }
            ShowMessage(ret.msg);
        }
    }
}